//具体实现
#include "scatter.h"
#include<QDebug>

#define WIN_SIZE 400

Scatter::Scatter(QWidget *parent) : QWidget(parent)
  ,xSlam(0)
  ,ySlam(0)
  ,xPosPre(0)
  ,yPosPre(0)
  ,xPosPro(0)
  ,yPosPro(0)
{
    //该类需要指定一个父窗口
    parent->resize(WIN_SIZE,WIN_SIZE);          // this->
    auto XRange=MAX_X-MIN_X;
    auto YRange=MAX_Y-MIN_Y;
    XScale=WIDTH_X/(double)XRange; //坐标系数，用宽/高来除以该坐标轴的最大值-最小值得到一个系数，然后用实际的数据X这个系数得到的就是实际坐标轴上的坐标了
    YScale=HEIGTH_Y/(double)YRange;
    mLabel=new QLabel(parent); //该控件用来装图片，后面用于显示  this
    mLabel->setGeometry(0,0,WIN_SIZE,WIN_SIZE);
    //mXAisle=new QComboBox(this);
   // mXAisle->setGeometry(20,250,50,20);

   // mYAisle=new QComboBox(this);
  //  mYAisle->setGeometry(0,10,75,35);
   // mXAisle->insertItem(0,"AAAAAAA");

    mPixMap=QPixmap(WIN_SIZE,WIN_SIZE);//初始化图片
    mPixMap.fill(Qt::darkGreen);
    QPainter painter(&mPixMap);
    painter.setRenderHint(QPainter::Antialiasing, true);//设置反锯齿模式
    QPen pen;
    pen.setColor(Qt::yellow);
    pen.setWidth(2);
    painter.setPen(pen);
    painter.drawLine(ORIGIN_X,ORIGIN_Y,ORIGIN_X+WIDTH_X,ORIGIN_Y);//X轴 自己需要定义一个原点 但是此时的实际坐标还是左上角，后面进行数据处理时，需要进行坐标转换，画家画线，起点是自己定义的坐标，终点是自己定义的坐标加上自己定义X,Y轴的长度，这个时候得到X Y轴
    painter.drawLine(ORIGIN_X,ORIGIN_Y-HEIGTH_Y,ORIGIN_X,ORIGIN_Y);//Y轴

    //绘制原点 X 绘制原点的0
    painter.drawLine(ORIGIN_X,ORIGIN_Y,ORIGIN_X,ORIGIN_Y+5);
    painter.drawText(ORIGIN_X,ORIGIN_Y+15,QString::number(MIN_X));
    //绘制原点 Y
    painter.drawLine(ORIGIN_X-5,ORIGIN_Y,ORIGIN_X,ORIGIN_Y);
    painter.drawText(ORIGIN_X-15,ORIGIN_Y,QString::number(MIN_Y));
    //绘制X轴 绘制X轴的刻度线，分几个刻度就遍历几次，
    int N=5;
    for(int i=0;i<N;i++)
    {
     painter.drawLine(ORIGIN_X+(i+1)*(WIDTH_X/N),ORIGIN_Y,ORIGIN_X+(i+1)*(WIDTH_X/N),ORIGIN_Y+5);
     painter.drawText(ORIGIN_X+(i+1)*(WIDTH_X/N)-20,ORIGIN_Y+20,QString::number(MIN_X+(i+1)*(XRange/N)));
    }
    //绘制Y轴
    for(int i=0;i<N;i++)
    {
     painter.drawLine(ORIGIN_X-5,ORIGIN_Y-(i+1)*(HEIGTH_Y/N),ORIGIN_X,ORIGIN_Y-(i+1)*(HEIGTH_Y/N));
     painter.translate(ORIGIN_X-15,ORIGIN_Y-(i+1)*(HEIGTH_Y/N)+5);
     painter.rotate(-90);
     painter.drawText(-10,0,QString::number(MIN_Y+(i+1)*(YRange/N)));
     painter.resetTransform();
    }

    mLabel->setPixmap(mPixMap);//这是把图片设置到控件中去，从而达到显示的目的
    mLabel->adjustSize();//自适应大小

//    one=new QTimer();   // for testing
//    connect(one,SIGNAL(timeout()),this,SLOT(PlotSCatter()));
//    one->start(1000);

}

uint16_t Scatter::getXSlam(){    return this->xSlam;}
uint16_t Scatter::getYSlam(){    return this->ySlam;}
uint16_t Scatter::getXPosPre(){    return this->xPosPre;}
uint16_t Scatter::getYPosPre(){    return this->yPosPre;}
uint16_t Scatter::getXPosPro(){    return this->xPosPro;}
uint16_t Scatter::getYPosPro(){    return this->yPosPro;}
void Scatter::setXSlam(uint16_t xSlam){    this->xSlam = xSlam;}
void Scatter::setYSlam(uint16_t ySlam){    this->ySlam = ySlam;}
void Scatter::setXPosPre(uint16_t xPosPre){    this->xPosPre = xPosPre;}
void Scatter::setYPosPre(uint16_t yPosPre){    this->yPosPre = yPosPre;}
void Scatter::setXPosPro(uint16_t xPosPro){    this->xPosPro = xPosPro;}
void Scatter::setYPosPro(uint16_t yPosPro){    this->yPosPro = yPosPro;}

void Scatter::PlotSCatter(uint16_t *xSlam, uint16_t *ySlam)
{
    QPainter painter(&mPixMap);
    QPen pen;
    pen.setColor(Qt::yellow);
    pen.setWidth(1);
    painter.setPen(pen);

//    QTime time= QTime::currentTime();
//    qsrand(time.msec()+time.second()*1000);
//    double x=qrand()%MAX_X;
//    double y=qrand()%MAX_Y;
    double x = *xSlam, y = *ySlam;
//    qDebug()<<x<<y;
    painter.drawPoint(ORIGIN_X+(x-MIN_X)*XScale, ORIGIN_Y-(y-MIN_Y)*YScale);
    mLabel->setPixmap(mPixMap);
    mLabel->adjustSize();
}


void Scatter::DrawLines(uint16_t *xDrone, uint16_t *yDrone)
{
    QPainter painter(&mPixMap);
    QPen pen;
    pen.setColor(Qt::red);
    pen.setWidth(4);
    painter.setPen(pen);

//    QTime time= QTime::currentTime();
//    qsrand(time.msec()+time.second()*1000);
//    double x=qrand()%MAX_X;
//    double y=qrand()%MAX_Y;
    double x = *xDrone, y = *yDrone;
//    qDebug()<<x<<y;
    painter.drawPoint(ORIGIN_X+(x-MIN_X)*XScale, ORIGIN_Y-(y-MIN_Y)*YScale);
    mLabel->setPixmap(mPixMap);
    mLabel->adjustSize();
}
